﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RCLIB2014;
using RCLIB2014.Build;
using RCLIB2014.Ride;
using RCLIB2014.Types;
using System.Timers;
using System.Diagnostics;
namespace ConsoleRC
{
    class RC
    {
        public void Start()
        {
            RollerCoasterMaker game = new RollerCoasterMaker();
            Builder builder = game.Builder(game.NewCoaster());
            Rider rider = new Rider();
            Menu(builder, rider);
        }

        public void Menu(Builder builder, Rider rider)
        {
            TaskResults taskResults = builder.LastTaskResults();

            char keyPressed = '-';
            do
            {
                Console.WriteLine("(1) Build Stright");
                Console.WriteLine("(2) Build Left (3) Build Right");
                Console.WriteLine("(4) Build Up (5) Build Down");
                Console.WriteLine("(6) Build Upward (7) Build Downward");
                Console.WriteLine("(8) Build Loop (9) Build Flat");
                Console.WriteLine("(F) Finsh Coaster");
                Console.WriteLine("(S) Ride Coaster");
                Console.WriteLine("(R) Remove Chunk");
                Console.WriteLine("(B) Exit");
                Console.WriteLine("");
                if (taskResults.Task != null) 
                {
                    Console.WriteLine("Task Ran: {0}", taskResults.Task);
                    if (taskResults.RuleBroke != null)
                        Console.WriteLine("Rule Broke: {0}", taskResults.RuleBroke);
                    else
                        Console.WriteLine("Rule Broke: - ");

                    if (taskResults.FixTask != null)
                        Console.WriteLine("Fix Task: {0}", taskResults.FixTask);
                    else
                        Console.WriteLine("Fix Task: - ");

                    Console.WriteLine("Pass: {0}", taskResults.Pass);

                    if (taskResults.Addtional != null)
                        Console.WriteLine("Addtional Task: {0}", taskResults.Addtional);
                    else
                        Console.WriteLine("Addtional Task: - ");
                    Console.WriteLine("");
                    Console.WriteLine("Last Chunk of Tracks");
                    DisplayLastChunk(builder.GetCoaster());
                    Console.WriteLine("");
                }

                Console.WriteLine("");
                Console.Write("Press Key To Perfrom Action:");
                keyPressed = Console.ReadKey().KeyChar;



                if (keyPressed == '1')
                    taskResults = builder.BuildStraight();
                else if (keyPressed == '2')
                    taskResults = builder.BuildLeft();
                else if (keyPressed == '3')
                    taskResults = builder.BuildRight();
                else if (keyPressed == '4')
                    taskResults = builder.BuildUp();
                else if (keyPressed == '5')
                    taskResults = builder.BuildDown();
                else if (keyPressed == '6')
                    taskResults = builder.BuildUpward();
                else if (keyPressed == '7')
                    taskResults = builder.BuildDownward();
                else if (keyPressed == '8')
                    taskResults = builder.BuildLoop();
                else if (keyPressed == '9')
                    taskResults = builder.BuildFlat();
                else if (Char.ToLower(keyPressed) == 'r')
                    taskResults = builder.RemoveChunk();
                else if (Char.ToLower(keyPressed) == 'f')
                    taskResults = builder.FinshCoaster();
                else if (Char.ToLower(keyPressed) == 's')
                {
                    TimeSpan last = new TimeSpan();
                    Camera camera = new Camera();
                    Stopwatch stopWatch = new Stopwatch();
                    rider.cart = new Object3D();
                    rider.GotoTrack(builder.GetCoaster().GetCurrentTracks, 0);
                    rider.RideCoaster();
                    stopWatch.Start();
                    while (rider.IsActive)
                    {
                        TimeSpan elaspedTime = new TimeSpan(stopWatch.ElapsedTicks);
                        rider.contineRide(builder.GetCoaster().GetCurrentTracks, camera, elaspedTime);
                        stopWatch.Restart();
                        System.Threading.Thread.Sleep(100);
                        Console.Clear();
                        Console.WriteLine(rider.cart);
                    }
                }
                //taskResults =
                else
                {
                    taskResults = new TaskResults(null);
                    Console.Clear();
                    Console.WriteLine("Unknown or uncompleted command, please any key to reload menu.");
                    Console.ReadKey();
                }

                Console.Clear();


            } while (keyPressed != 'B');
        }

        public void DisplayLastChunk(Coaster coaster)
        {
            var tracks = coaster.GetCurrentTracks;
            var chunks = coaster.GetCurrentChunks;

            if (tracks != null && chunks != null && chunks.Count > 0 && tracks.Count > 0)
            {
                for (int i = tracks.Count - chunks.Last(); i < tracks.Count; i++)
                {
                    Console.WriteLine(tracks[i]);
                }
            }
            else
                Console.WriteLine("No Chunks Bulit");
        }
    }
}
